package com.itlong.cloud.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.PO.DevicePadAuthCodePO;
import com.itlong.cloud.POJO.PO.DevicePadBridgeInfoPO;
import com.itlong.cloud.POJO.PO.DevicePadInfoPO;
import com.itlong.cloud.POJO.PO.SpecialDevicePadInfoPO;
import com.itlong.cloud.POJO.VO.app.*;
import com.itlong.cloud.POJO.VO.operate.OperateUpdateDetailVO;
import com.itlong.cloud.POJO.VO.property.*;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.app.dao.IAppPadFaceDao;
import com.itlong.cloud.app.service.IAppPadFaceService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.enumerate.DeviceIotEnum;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * <desc>
 * 平板人脸接口服务层实现类
 * </desc>
 *
 * @createDate 2018/05/14
 */
@Service
public class AppPadFaceServiceImpl implements IAppPadFaceService {
    private static final Logger LOG = LoggerFactory.getLogger(AppPadFaceServiceImpl.class);

    @Autowired
    IAppPadFaceDao iAppPadFaceDao;

    @Autowired
    MessageSender messageSender;

    /**
     * <desc>
     * 判断项目是否禁用。
     * <desc/>
     *
     * @param projectId 项目编号
     * @return
     * @author Qiang.S
     * @createDate 2018-06-13
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer checkProjectForbidden(String projectId) throws Exception {
        return iAppPadFaceDao.checkProjectForbidden(projectId);
    }

    /**
     * <desc>
     * 设备验证(梯控)。
     * <desc/>
     *
     * @param projectId         项目编号
     * @param deviceId          项目ID
     * @param elevatorType      电梯类型
     * @param isFaceRecognition 是否支持人脸识别
     * @return
     * @author Qiang.S
     * @createDate 2018-05-14
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public String elevatorAuthorityCheck(String projectId, String deviceId, String elevatorType, String isFaceRecognition) throws Exception {
        StringBuilder status = new StringBuilder();
        for (String deviceIdOne : deviceId.split(",")) {
            status.append(iAppPadFaceDao.elevatorAuthorityCheck(projectId, deviceIdOne, elevatorType, isFaceRecognition) == null ?
                    0 : iAppPadFaceDao.elevatorAuthorityCheck(projectId, deviceIdOne, elevatorType, isFaceRecognition) + ",");
        }
        return status.toString().substring(0, status.length() - 1);
    }

    /**
     * <desc>
     * 设备验证(群控器)。
     * <desc/>
     *
     * @param projectId 项目编号
     * @param deviceId  项目ID
     * @return
     * @author Qiang.S
     * @createDate 2018-05-14
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public String controllerAuthorityCheck(String projectId, String deviceId) throws Exception {
        StringBuilder status = new StringBuilder();
        for (String deviceIdOne : deviceId.split(",")) {
            status.append(iAppPadFaceDao.controllerAuthorityCheck(projectId, deviceIdOne) == null ?
                    0 : iAppPadFaceDao.controllerAuthorityCheck(projectId, deviceIdOne) + ",");
        }
        return status.toString().substring(0, status.length() - 1);
    }

    /**
     * <desc>
     * 设备验证(门禁)。
     * <desc/>
     *
     * @param projectId         项目编号
     * @param deviceId          项目ID
     * @param isFaceRecognition 是否支持人脸识别
     * @return
     * @author Qiang.S
     * @createDate 2018-05-14
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public String entranceAuthorityCheck(String projectId, String deviceId, String isFaceRecognition) throws Exception {
        StringBuilder status = new StringBuilder();
        for (String deviceIdOne : deviceId.split(",")) {
            status.append(iAppPadFaceDao.entranceAuthorityCheck(projectId, deviceIdOne, "") == null ?
                    0 : iAppPadFaceDao.entranceAuthorityCheck(projectId, deviceIdOne, "") + ",");
        }
        return status.toString().substring(0, status.length() - 1);
    }

    /**
     * <desc>
     * 登陆验证。
     * <desc/>
     *
     * @param projectId 项目编号
     * @param userName  账号
     * @param password  密码
     * @return
     * @author Qiang.S
     * @createDate 2018-05-14
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer engineerLoginVerify(String projectId, String userName, String password) throws Exception {
        return iAppPadFaceDao.engineerLoginVerify(projectId, userName, password);
    }

    /**
     * <desc>
     * 登陆项目验证。
     * <desc/>
     *
     * @param projectId 项目编号
     * @param userName  账号
     * @return
     * @author Qiang.S
     * @createDate 2018-06-08
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer engineerLoginProjectVerify(String projectId, String userName) throws Exception {
        return iAppPadFaceDao.engineerLoginProjectVerify(projectId, userName);
    }

    /**
     * <desc>
     * 工程人员帐号是否存在验证。
     * <desc/>
     *
     * @param userName 账号
     * @return
     * @author Qiang.S
     * @createDate 2018-06-11
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer engineerLoginExistsVerify(String userName) throws Exception {
        return iAppPadFaceDao.engineerLoginExistsVerify(userName);
    }

    /**
     * <desc>
     * 平板获取广告(梯控)。
     * <desc/>
     *
     * @param projectId 项目编号
     * @param deviceId  项目ID
     * @return
     * @author Qiang.S
     * @createDate 2018-05-14
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public List<AppPadADVO> getElevatorAD(String projectId, String deviceId) throws Exception {
        List<AppPadADVO> list = new ArrayList<>();
        for (String deviceIdStr : deviceId.split(",")) {
            list.addAll(iAppPadFaceDao.getElevatorAD(projectId, deviceIdStr));
        }
        return list;
    }

    /**
     * <desc>
     * 平板获取广告(门禁)。
     * <desc/>
     *
     * @param projectId 项目编号
     * @param deviceId  项目ID
     * @return
     * @author Qiang.S
     * @createDate 2018-05-14
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public List<AppPadADVO> getEntranceAD(String projectId, String deviceId) throws Exception {
        List<AppPadADVO> list = new ArrayList<>();
        for (String deviceIdStr : deviceId.split(",")) {
            list.addAll(iAppPadFaceDao.getEntranceAD(projectId, deviceIdStr));
        }
        return list;
    }

    /**
     * <desc>
     * 通过projectId和deviceId获取deviceUnique。
     * <desc/>
     *
     * @param type      设备类型
     * @param projectId 项目编号
     * @param deviceId  设备机号
     * @return
     * @author Qiang.S
     * @createDate 2018-05-16
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public AppPadDeviceVO getDeviceUnique(String type, String projectId, String deviceId) throws Exception {
        AppPadDeviceVO appPadDeviceVO = null;
        if ("1".equals(type)) {//梯控
            appPadDeviceVO = iAppPadFaceDao.getElevatorDeviceUnique(projectId, deviceId);
        }
        if ("2".equals(type)) {//门禁
            appPadDeviceVO = iAppPadFaceDao.getEntranceDeviceUnique(projectId, deviceId);
        }
        return appPadDeviceVO;
    }

    /**
     * <desc>
     * 获取用户账户金额。
     * <desc/>
     *
     * @param userId 用户编号
     * @return
     * @author shengen.T
     * @createDate 2018-05-24
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
    public BigDecimal getUserAccountMoney(String userId) throws Exception {
        return iAppPadFaceDao.getUserAccountMoney(userId);
    }

    /**
     * <desc>
     * 获取平板更新信息。
     * <desc/>
     *
     * @param type 更新类型；1为人脸识别客户端，2为人脸识别服务端
     * @return
     * @author Qiang.S
     * @createDate 2018-05-31
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
    public AppPadProgramInfoVO getPadProgramInfo(String type) throws Exception {
        type = StringUtils.isEmpty(type) ? "0" : type;
        return iAppPadFaceDao.getPadProgramInfo(type);
    }

    /**
     * <desc>
     * 判断当前平板设备唯一码的设备是否存在或是否被禁用。
     * <desc/>
     *
     * @param padDeviceUnique 设备唯一码
     * @return
     * @author Juguang.S
     * @createDate 2019-01-18
     */
    @Override
    public String getPadDeviceExist(String padDeviceUnique) {
        return iAppPadFaceDao.getPadDeviceExist(padDeviceUnique);
    }

    /**
     * <desc>
     * 根据人脸编号获取电梯信息。
     * <desc/>
     *
     * @param uId 百度人脸编号
     * @return
     * @author Qiang.S
     * @createDate 2019-02-27
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
    public List<AppPadVisitorElevatorInfoVO> getElevatorInfoByFaceId(String uId) throws Exception {
        return iAppPadFaceDao.getElevatorInfoByFaceId(uId);
    }

    /**
     * <desc>
     * 根据baiduId来更新绑定百度识别授权码的设备信息。
     * <desc/>
     *
     * @param projectId       项目编号
     * @param emId            emId
     * @param baiduId         百度ID
     * @param padDeviceUnique 平板设备唯一码
     * @param requestType     请求类型  1代表离线验证接口调用  2代表绑定激活码接口调用
     * @return 百度识别码
     * @author Juguang.S
     * @createDate 2019-01-18
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public String bingBaiDuAuthCode(String projectId, String emId, String baiduId, String padDeviceUnique, String requestType) throws Exception {
        //先判断该emId是否已绑定过百度识别授权码
        Map<String, Object> mapResult = iAppPadFaceDao.getIsBingBaiduCode(baiduId);
        if (null != mapResult) {
            String oldProjectId = "";
            String oldDeviceUnique = "";
            if (mapResult.size() > 2) {
                oldProjectId = mapResult.get("projectId").toString();
                oldDeviceUnique = mapResult.get("deviceUnique").toString();
            }
            String baiduAuthCode = mapResult.get("baiduAuthCode").toString();
            String useStatus = mapResult.get("useStatus").toString();
            if (!projectId.equals(oldProjectId) || !padDeviceUnique.equals(oldDeviceUnique)) {
                //处理设备坏了该BaiduId所绑定的激活码也不能正常使用，但后期又修好了，需1.释放该机号与之前所绑定百度id激活码之间的关系 2.将该激活码的状态改为已使用
                if (useStatus.equals("0") || iAppPadFaceDao.getDeviceBuyOrBingStatus(padDeviceUnique) > 0) {
                    iAppPadFaceDao.updateOldDeviceBingInfo(padDeviceUnique);
                    useStatus = "2";
                }
                //则需更新项目编号和设备唯一码
                iAppPadFaceDao.updateProjectId(projectId, baiduId, padDeviceUnique, useStatus);
            }
            return baiduAuthCode;
        }
        if ("2".equals(requestType)) {
            Map<String, Object> mapInfo = null;
            //未绑定则查询未绑定的百度识别授权码完成绑定
            Map<String, Object> map = iAppPadFaceDao.getBaiduCode(padDeviceUnique);
            if (map == null || map.size() == 0) {
                //判断是否由于百度识别授权码库存不足 若不是则将更新该设备的绑定关系
                mapInfo = iAppPadFaceDao.isEnoughByBaiduCode();
                if (mapInfo == null || mapInfo.size() == 0) {
                    return "";
                } else {
                    iAppPadFaceDao.updateOldDeviceBingInfo(padDeviceUnique);
                }
            }

            DevicePadAuthCodePO devicePadAuthCodePO = new DevicePadAuthCodePO();
            if (mapInfo != null) {
                devicePadAuthCodePO.setCodeId(mapInfo.get("codeId").toString());
                devicePadAuthCodePO.setBaiduAuthCode(mapInfo.get("baiduAuthCode").toString());
            } else {
                devicePadAuthCodePO.setCodeId(map.get("codeId").toString());
                devicePadAuthCodePO.setBaiduAuthCode(map.get("baiduAuthCode").toString());
            }
            devicePadAuthCodePO.setProjectId(projectId);
            devicePadAuthCodePO.setDeviceUnique(padDeviceUnique);
            devicePadAuthCodePO.setEmId(emId);
            devicePadAuthCodePO.setBaiduId(baiduId);
            devicePadAuthCodePO.setUseTime(new Date());
            devicePadAuthCodePO.setUseStatus(2);
            devicePadAuthCodePO.setUpdateTime(new Date());
            Map<String, Object> params = SqlUtil.durableData(devicePadAuthCodePO, PlatformConstants.TABLE_UPDATE);
            iAppPadFaceDao.update(params);
            if (mapInfo != null) {
                return mapInfo.get("baiduAuthCode").toString();
            }
            return map.get("baiduAuthCode").toString();
        }
        return null;
    }

    /**
     * <desc>
     * 根据baiduId来更新绑定百度识别授权码的设备信息。
     * <desc/>
     *
     * @param baiduId 百度ID
     * @param code    3激活成功4激活失败
     * @param msg     失败信息
     * @return 百度识别码
     * @author Qiang.S
     * @createDate 2019-08-06
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer reportBaiDuAuthCode(String baiduId, String code, String msg) throws Exception {
        return iAppPadFaceDao.reportBaiDuAuthCode(baiduId, code, msg);
    }

    /**
     * <desc>
     * 校验该平板机号有没被其它设备占用
     * <desc/>
     *
     * @param baiduId         百度ID
     * @param padDeviceUnique 平板设备唯一码
     * @return
     * @author Juguang.S
     * @createDate 2019-03-20
     */
    @Override
    public Integer isDeviceIdUserd(String baiduId, String padDeviceUnique) {
        return iAppPadFaceDao.isDeviceIdUserd(baiduId, padDeviceUnique);
    }

    /**
     * <desc>
     * 根据人脸编号获取门禁信息。
     * <desc/>
     *
     * @param uId 百度人脸编号
     * @return
     * @author Qiang.S
     * @createDate 2019-02-27
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
    public List<AppDoorDevicesVO> getDoorInfoByFaceId(String uId) throws Exception {
        return iAppPadFaceDao.getDoorInfoByFaceId(uId);
    }

    /**
     * <desc>
     * 获取项目的设备过期时间。
     * <desc/>
     *
     * @param projectId 项目编号
     * @return
     * @author Juguang.S
     * @createDate 2019-08-06
     */
    @Override
    public List<Map<String, Object>> getDeviceCloudServiceCostEndTime(String projectId) {
        List<Map<String, Object>> deviceCloudServiceCostEndTime = iAppPadFaceDao.getDeviceCloudServiceCostEndTime(projectId);
        return deviceCloudServiceCostEndTime == null ? new ArrayList<>() : deviceCloudServiceCostEndTime;
    }

    /**
     * <desc>
     * 获取物业的联系方式。
     * <desc/>
     *
     * @param projectId 项目编号
     * @return
     * @author Juguang.S
     * @createDate 2019-08-15
     */
    @Override
    public String getPropertyPhone(String projectId) {
        return iAppPadFaceDao.getPropertyPhone(projectId);
    }

    /**
     * <desc>
     * 注册平板iot。
     * <desc/>
     *
     * @param padDeviceUnique 平板机号
     * @return
     * @author Qiang.Sgit s
     * @createDate 2019-07-10
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
    public CloudElevatorVO registerPadIot(String padDeviceUnique) throws Exception {
        CloudElevatorVO vo = iAppPadFaceDao.getPadIotInfo(padDeviceUnique);
        if (vo != null && vo.getIotDeviceSecret() == null) {
            Map<String, Object> params = new HashMap<>();
            params.put("deviceOperateIotType", DeviceIotEnum.REGIST_DEVICE.getCommandType());
            params.put("productKey", vo.getIotProductKey());
            params.put("deviceUnique", padDeviceUnique);
            Map<String, Object> result = new HashMap<>();
            result.put("data", params);
            messageSender.send(RabbitMQConstant.RABBITMQ_DEVICE_ELEVATOR_QUEUE, JsonUtil.toJSON(result),
                    RabbitMQConstant.RABBITMQ_DEVICE_ELEVATOR_EXCHANGE, RabbitMQConstant.RABBITMQ_DEVICE_ELEVATOR_KEY);
        }
        return vo;
    }

    /**
     * <desc>
     * 设备升级。
     * <desc/>
     *
     * @param deviceType   升级包类型；4 维护程序，5 业务程序，6 桌面程序，7 通信程序
     * @param deviceUnique 设备编号
     * @return
     * @author Qiang.S
     * @createDate 2019/12/06
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
    public List<CloudDeviceUpdateInfoVO> deviceUpdate(String deviceUnique, String deviceType) throws Exception {
        List<CloudIntercomChangeLogOrderVO> list = iAppPadFaceDao.getUpdateDeviceInfo(deviceUnique);

        if (list.size() == 0) {//无此设备升级命令
            return null;
        }
        StringBuffer ids = new StringBuffer();
        //取消所有沉余指令
        for (int i = 1; i < list.size(); i++) {
            ids.append(list.get(i).getId().toString() + ",");
        }

        List<OperateUpdateDetailVO> operateUpdateDetailVOS = iAppPadFaceDao.getUpdateDetailForDeviceUpdate(list.get(0).getCommandData().toString(), deviceType, "5");
        if (operateUpdateDetailVOS == null || operateUpdateDetailVOS.size() == 0) {
            return null;
        }
        List<CloudDeviceUpdateInfoVO> voList = new ArrayList<>();
        for (OperateUpdateDetailVO operateUpdateDetailVO : operateUpdateDetailVOS) {
            CloudDeviceUpdateInfoVO cloudDeviceUpdateInfoVO = new CloudDeviceUpdateInfoVO();
            cloudDeviceUpdateInfoVO.setCommandId(list.get(0).getId().toString());
            cloudDeviceUpdateInfoVO.setFileName(operateUpdateDetailVO.getUpdateName());
            cloudDeviceUpdateInfoVO.setMd5(operateUpdateDetailVO.getMd5());
            cloudDeviceUpdateInfoVO.setVersionCode(operateUpdateDetailVO.getVersion());
            cloudDeviceUpdateInfoVO.setUrl(operateUpdateDetailVO.getUpdateUrl());
            cloudDeviceUpdateInfoVO.setFilePackage(operateUpdateDetailVO.getFilePackage());
            voList.add(cloudDeviceUpdateInfoVO);
        }

        //将执行的指令调整为 处理中
        String[] newId = {list.get(0).getId().toString()};
        iAppPadFaceDao.changeChangeLogOrdersStatus(deviceUnique, newId, 2);
        return voList;
    }

    /**
     * <desc>
     * 根据deviceUnique批量获取设备地址
     * </desc>
     *
     * @param bindDeviceUnique 平板绑定的的deviceUnique，多个以逗号隔开
     * @param padDeviceUnique
     * @return
     * @Author Zhu.yj
     * @createDate 2019/12/16
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer updatePadBridgeInfo(String bindDeviceUnique, String padDeviceUnique, String projectId, String mac) throws Exception {
        List<String> deviceUniqueList = StringHandlerUtil.splitStringList(bindDeviceUnique);
        //将绑定的设备编号和mac地址存入平板信息中
        iAppPadFaceDao.updatePadInfo(bindDeviceUnique, padDeviceUnique, mac);
        List<Map<String, Object>> deviceAddressList;
        Integer type = Integer.parseInt(deviceUniqueList.get(0).substring(8, 9));
        switch (PropertyEquipTypeEnum.getByEquipType(type)) {
            case ELEVATOR:
                deviceAddressList = iAppPadFaceDao.getElevatorAddressList(deviceUniqueList);
                break;
            case ENTRANCE:
                deviceAddressList = iAppPadFaceDao.getEntranceAddressList(deviceUniqueList);
                break;
            default:
                deviceAddressList = iAppPadFaceDao.getCloudControllerAddressList(deviceUniqueList);
                break;
        }
        //绑定地址时删除平板之前的地址
        iAppPadFaceDao.batchDeleteDeviceBridgeInfo(padDeviceUnique);
        //将绑定的设备的地址添加为平板的地址
        List<Map<String, Object>> batchSaveParams = new ArrayList<>();
        if (deviceAddressList.size() != 0) {
            for (Map<String, Object> deviceAddress : deviceAddressList) {
                DevicePadBridgeInfoPO devicePadBridgeInfoPO = new DevicePadBridgeInfoPO();
                devicePadBridgeInfoPO.setDeviceUnique(padDeviceUnique);
                devicePadBridgeInfoPO.setProjectId(projectId);
                devicePadBridgeInfoPO.setBuildId(deviceAddress.get("buildId").toString());
                devicePadBridgeInfoPO.setUnitId(deviceAddress.get("unitId").toString());
                devicePadBridgeInfoPO.setCreateTime(new Date());
                devicePadBridgeInfoPO.setUpdateTime(new Date());
                batchSaveParams.add(SqlUtil.durableData(devicePadBridgeInfoPO, PlatformConstants.TABLE_SAVE));
            }
            Integer result = iAppPadFaceDao.batchSavePadBridgeInfo(batchSaveParams);
            return result;
        }
        return 0;
    }

    /**
     * <desc>
     * 获取平板绑定设备数量
     * </desc>
     *
     * @param bindDeviceUniques
     * @return
     * @Author Zhu.yj
     * @createDate 2020/1/2
     */
    @Override
    public Integer getBindDeviceCount(String bindDeviceUniques, String deviceType) throws Exception {
        List<String> bindDeviceUniqueList = StringHandlerUtil.splitStringList(bindDeviceUniques);
        Integer deviceCount;
        if (PropertyEquipTypeEnum.ELEVATOR.getType().toString().equals(deviceType)) {
            deviceCount = iAppPadFaceDao.getBindElevatorCount(bindDeviceUniqueList);
        } else if (PropertyEquipTypeEnum.ENTRANCE.getType().toString().equals(deviceType)) {
            deviceCount = iAppPadFaceDao.getBindEntranceCount(bindDeviceUniqueList);
        } else {
            deviceCount = iAppPadFaceDao.getCloudControllerCount(bindDeviceUniqueList);
        }
        return deviceCount;
    }

    /**
     * <desc>
     * 校验要绑定的设备中是否有普通门禁
     * </desc>
     *
     * @param bindDeviceUniques 绑定的门禁设备编号
     * @return 普通门禁数量
     * @Author Zhu.yj
     * @createDate 2020/1/8
     */
    @Override
    public Integer checkCommonEntranceExist(String bindDeviceUniques) throws Exception {
        List<String> entranceDeviceList = StringHandlerUtil.splitStringList(bindDeviceUniques);
        return iAppPadFaceDao.getCommonEntranceCount(entranceDeviceList);
    }

    /**
     * <desc>
     * 根据平板绑定普通门禁编号处理数据
     * </desc>
     *
     * @param bindDeviceUniques 绑定的门禁设备编号
     * @return 普通门禁数量
     * @Author Qiang.S
     * @createDate 2020/02/20
     */
    @Override
    public Map<String, Object> getUserInfoByRoleId(String bindDeviceUniques) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<PadFaceUserInfoNewVO> all = new ArrayList<>();
        List<PadFaceUserInfoNewVO> vos;
        List<String> bindDeviceUniqueList = StringHandlerUtil.splitStringList(bindDeviceUniques);
        List<Map<String, Object>> auths = new ArrayList<>();
        for (String bindDeviceId : bindDeviceUniqueList) {
            //根据设备编号查询绑定了的角色编号,查询出所有的用户信息
            vos = iAppPadFaceDao.getUserInfoByRoleId(bindDeviceId);
            //组装vip标识字段
            for (PadFaceUserInfoNewVO newVO : vos) {
                if (newVO.getUserLabel()==null){
                    newVO.setIsVip(0);
                }else {
                    newVO.setIsVip(Arrays.asList(newVO.getUserLabel().split(",")).contains("1")?1:0);
                }
            }

            all.addAll(vos);
            for (PadFaceUserInfoNewVO vo : vos) {
                Map<String, Object> auth = new HashMap<>();
                List<Map<String, Object>> sysAuths = new ArrayList<>();
                auth.put("userId", vo.getUserId());
                auth.put("houseUserType", vo.getHouseUserType());
                auth.put("deviceType", 1);
                Map<String, Object> sysAuth = new HashMap<>();
                sysAuth.put("auth", "8");
                sysAuth.put("deviceUnique", bindDeviceId);
                sysAuths.add(sysAuth);
                auth.put("sysAuths", sysAuths);
                auths.add(auth);
            }
        }
        result.put("userInfos", all);
        result.put("devices", auths);
        return result;
    }

    /**
     * <desc>
     * 平板人脸识别仪上报设备的能力集信息
     * </desc>
     *
     * @param params 信息
     * @Author pengwei
     * @createDate 2020/11/16
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void uploadDeviceInfo(Map<String,String> params) throws Exception {
        String deviceUnique = params.get("deviceUnique");
        String deviceAttributes  =  params.get("deviceAttributes");
        String status = iAppPadFaceDao.getPadDeviceExist(deviceUnique);
        String  productCode  =    iAppPadFaceDao.getProductCode(deviceAttributes);
        params.put("productCode",productCode);
        if (StringUtils.isNotEmpty(status)) {
            //设备存在,更新主表
            iAppPadFaceDao.updateDeviceInfo(params);
        } else {
            //更新特殊设备表   'launcherVersion: ',deiall.face_launcher),('pluginVersion: ',deiall.face_plugin))  linuxAppVer,
            params.put("facePlugin",( "launcherVersion:"+params.get("faceLauncher")+","+"pluginVersion:"+ params.get("facePlugin")));
            iAppPadFaceDao.updateSpecialDeviceInfo(params);
        }
    }

    /**
     * <desc>
     * 获取设备是否存在
     * </desc>
     *
     * @param deviceUnique
     * @return
     * @author caoshuai
     * @createDate 2020/12/10 11:52
     */
    @Override
    public DevicePadInfoPO getDevicePadByDeviceUnique(String deviceUnique) {

        return iAppPadFaceDao.getDevicePadByDeviceUnique(deviceUnique);
    }

    /**
     * <desc>
     * 保存
     * </desc>
     *
     * @param specialDevicePadInfoPO
     * @return
     * @author caoshuai
     * @createDate 2020/12/10 11:52
     */
    @Override
    public Integer save(SpecialDevicePadInfoPO specialDevicePadInfoPO) throws Exception {

        return iAppPadFaceDao.save(SqlUtil.durableData(specialDevicePadInfoPO, PlatformConstants.TABLE_SAVE));
    }
}
